package com.sndscl.rpc.spring;

import com.sndscl.rpc.SandSculptureServer;
import org.apache.zookeeper.KeeperException;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Description
 * @Author lsh
 * @Date 2020/10/16 14:10
 **/
@Component
public class ServerStartCompileListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //web系统中会有两个容器，防止多次调用
        if(contextRefreshedEvent.getApplicationContext().getParent() == null){
            try {
                new SandSculptureServer().init();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
